package com.hyts.rpclet.serialize;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hyts.rpclet.constant.RpcletSerializeType;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.rpclet.serialize
 * author:Libo/Alex
 * create-date:2019-07-10 18:59
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于:
 */
@Component
public class JsonRpcletSerializer implements RpcletSerializer{


    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String getSerializeType() {
        return RpcletSerializeType.JSON.getContentType();
    }

    @Override
    public Object decode(InputStream inputBuf, Class parameterType) {
        try {
            return objectMapper.readValue(inputBuf,parameterType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void encode(OutputStream outputStream, Object value) {
        try {
            objectMapper.writeValue(outputStream,value);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
